package com.kazalan.content.persistence;

import java.util.List;

import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Restrictions;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.kazalan.domain.document.Language;

public class HibernateLanguageDaoImpl extends HibernateDaoSupport implements LanguageDao{

	@Override
	public void saveLanguage(Language language) {
		getHibernateTemplate().saveOrUpdate(language);
	}

	@Override
	public Language getLanguageByCode(String code) {
		DetachedCriteria criteria = DetachedCriteria.forClass(Language.class);
        criteria.add(Restrictions.eq("code", code));
        
        
        @SuppressWarnings("unchecked")
		List<Language> languages = getHibernateTemplate().findByCriteria(criteria);
        
        for(Language language : languages){
        	if(language.getCode().equals(code)){
        		return language;
        	}
        }
        
        return null;
	}

	@Override
	public List<Language> getLanguages() {
		return getHibernateTemplate().loadAll(Language.class);
	}

}
